package com.mengshu.platform.web.resp;

import com.mengshu.platform.config.Consts;
import lombok.Data;
import org.slf4j.MDC;

/**
 * <p>
 *     mvc统一返回
 * </p>
 *
 * @author liuning
 * @date 9:33 上午 2022/2/5
 */
@Data
public class R<T> {
    private long code;

    private String msg;

    private String traceid;

    private T data;

    public static <T> R<T> ok(T data) {
        return restResult(data, 0, "操作成功");
    }

    public static <T> R<T> error(long code, String msg) {
        return restResult(null, code, msg);
    }

    private static <T> R<T> restResult(T data, long code, String msg) {
        R<T> apiResult = new R();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        apiResult.setTraceid(MDC.get(Consts.TRACEID));
        return apiResult;
    }
}
